package portal.action.rest;

import beans.HttpResultList;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import portal.beans.HostMachine;
import portal.mybatis.dao.service.HostMachineService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
public class HostMachineAction {
    @Autowired
    HostMachineService hostMachineService;
    private HttpResultList result;

    @RequestMapping(value ="/HostMachineServlet",method = RequestMethod.POST)
    public void HostMachineServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String operateType = request.getParameter("operateType");
        try {
            switch (operateType){
                case "add":
                    addHostMachine(request,response);
                    break;
                case "delete":
                    deleteHostMachine(request,response);
                    break;
                case "alter":
                    alterHostMachine(request,response);
                    break;
                case "showAll":
                    showAllHostMachine(request,response);
                    break;
                case "showPaged":
                    showPaged(request,response);
                    break;
                case "showDetail":
                    showHostMachineDetail(request,response);
                    break;
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("POST参数错误");
            response.getWriter().print(JSONObject.fromObject(result).toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("POST数字错误");
        }
        response.getWriter().print(JSONObject.fromObject(result).toString());
    }

    private void addHostMachine(HttpServletRequest request, HttpServletResponse response) {
        String hostName = request.getParameter("hostName");
        String hostIp = request.getParameter("hostIp");
        String hostType = request.getParameter("hostType");
        Integer hostGroupId = Integer.parseInt(request.getParameter("hostGroupId"));
        String hostGroupName = request.getParameter("hostGroupName");
        String hostState = request.getParameter("hostState");
        String hostSSH = request.getParameter("hostSSH");
        HostMachine hostMachine = new HostMachine(hostName, hostIp, hostType, hostGroupId, hostGroupName, hostState, hostSSH);
        result = new HttpResultList();
        boolean res;
        if (hostMachineService.insertHost(hostMachine) == 1) {
            res = true;
        }
        else {
            res = false;
        }
        result.setResult(res);
    }

    private void deleteHostMachine(HttpServletRequest request, HttpServletResponse response) {
        Integer hostId = Integer.parseInt(request.getParameter("hostId"));
        result = new HttpResultList();
        boolean res;
        if (hostMachineService.deleteById(hostId) == 1) {
            res = true;
        }
        else {
            res = false;
        }
        result.setResult(res);
    }

    private void alterHostMachine(HttpServletRequest request, HttpServletResponse response) {
        Integer hostId = Integer.parseInt(request.getParameter("hostId"));
        String hostIp = request.getParameter("hostIp");
        String hostType = request.getParameter("hostType");
        String hostGroupName = request.getParameter("hostGroupName");
        Integer hostGroupId = Integer.parseInt(request.getParameter("hostGroupId"));
        String hostName = request.getParameter("hostName");
        String hostState = request.getParameter("hostState");
        String hostSSH = request.getParameter("hostSSH");
        HostMachine hostMachine = new HostMachine(hostId, hostName, hostIp, hostType, hostGroupId, hostGroupName, hostState, hostSSH);
        result = new HttpResultList();
        boolean res;
        if (hostMachineService.updateHostById(hostMachine) == 1){
            res = true;
        }
        else {
            res = false;
        }
        result.setResult(res);
    }

    private void showAllHostMachine(HttpServletRequest request, HttpServletResponse response) {
        result = new HttpResultList();
        List<HostMachine> hostMachineList = hostMachineService.showAll();
        List<Object> objects = new ArrayList<>();
        for(int i=0; i<hostMachineList.size(); i++){
            objects.add(hostMachineList.get(i));
        }
        result.setResult(hostMachineList.size()>0);
        result.setDatas(objects);
    }

    private void showPaged(HttpServletRequest request, HttpServletResponse response) {
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        List<HostMachine> hostMachineList = hostMachineService.showPaged(currentPage, onePageCount);
        result = new HttpResultList();
        List<Object> objects = new ArrayList<>();
        for(int i=0; i<hostMachineList.size(); i++){
            objects.add(hostMachineList.get(i));
        }
        result.setResult(hostMachineList.size()>0);
        result.setDatas(objects);
        if(onePageCount!=0&&currentPage!=0){
            Long allCount = hostMachineService.countAll();
            double res = Math.ceil((double)allCount / (double)onePageCount);
            result.setMessage((int)res+"");
        }
    }

    private void showHostMachineDetail(HttpServletRequest request, HttpServletResponse response) {
        Integer hostId = Integer.parseInt(request.getParameter("hostId"));
        List<HostMachine> hostMachineList = hostMachineService.selectById(hostId);
        result = new HttpResultList();
        List<Object> objects = new ArrayList<>();
        for(int i=0; i<hostMachineList.size(); i++){
            objects.add(hostMachineList.get(i));
        }
        result.setDatas(objects);
        result.setResult(hostMachineList.size()>0);
    }
}
